package com.magist.manage.email.service.impl;

import com.jeesuite.common.util.DateUtils;
import com.magist.core.util.DateUtil;
import com.magist.core.util.PrintOutUtil;
import com.magist.core.util.SendEmailUtils;
import com.magist.manage.copyrights.entity.Copyrights;
import com.magist.manage.email.service.EmailSender;
import com.magist.manage.enums.BalanceTypeEnum;
import com.magist.manage.enums.CopyrightsRangeEnum;
import com.magist.manage.enums.OnlineStatusEnum;
import com.magist.manage.enums.ProvideEnum;
import com.magist.manage.enums.TFEnum;
import com.magist.manage.enums.ValidStatusEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class CopyrightsEmailSenderImpl implements EmailSender<Copyrights> {



    @Override
    public void sendEmail(List<Copyrights> datas, String subject, String to) {
        if (!CollectionUtils.isEmpty(datas)) {
            StringBuilder content = new StringBuilder(
                    "<table border=\"1\" cellspacing=\"0\">" +
                            "<caption><h1>" + subject + "</h1></caption>" +
                            "<thead><tr>" +
                            "<th>ID</th>" +
                            "<th>唯一版权号</th>" +
                            "<th>出版社</th>" +
                            "<th>作品名称</th>" +
                            "<th>合同编号</th>" +
                            "<th>使用范围</th>" +
                            "<th>到期状态</th>" +
                            "<th>状态</th>" +
                            "<th>上线状态</th>" +
                            "<th>开始时间</th>" +
                            "<th>终止时间</th>" +
                            "<th>合作形式</th>" +
                            "<th>独家授权</th>" +
                            "<th>转授权</th>" +
                            "<th>原始版权</th>" +
                            "<th>操作人</th>" +
                            "<th>修改时间</th>" +
                            "<th>操作</th>" +
                            "</tr></thead>");
            datas.forEach(copyrights -> {

                Date now = new Date();
                Date endDate = copyrights.getEndDate();
                String expireStatus = null;
                int monthDiff = DateUtil.getMonthDiff(endDate, now);
                if (monthDiff == 0) {
                    expireStatus = "1个月内到期";
                } else if (monthDiff > 0 && monthDiff < 3) {
                    expireStatus = "3个月内到期";
                } else if (monthDiff >= 3 && monthDiff < 6) {
                    expireStatus = "6个月内到期";
                } else if (monthDiff >= 6) {
                    expireStatus = "6个月以外到期";
                }
                if (now.after(endDate)) {
                    expireStatus = "已到期";
                }
                content.append("<tr><td>" + copyrights.getShowId() + "</td>");
                content.append("<td>" + copyrights.getCopyrightsNo() + "</td>");
                content.append("<td>" + PrintOutUtil.printOut(copyrights.getPublishingName()) + "</td>");
                content.append("<td>" + PrintOutUtil.printOut(copyrights.getWorksName()) + "</td>");
                content.append("<td>" + PrintOutUtil.printOut(copyrights.getContractNo()) + "</td>");
                content.append("<td>" + (StringUtils.isEmpty(copyrights.getCopyrightsRange()) ? "" : Arrays.stream(copyrights.getCopyrightsRange().split(",")).map(range -> CopyrightsRangeEnum.getByCode(range).getDesc()).collect(Collectors.toList())) + "</td>");
                content.append("<td>" + expireStatus + "</td>");
                content.append("<td>" + ValidStatusEnum.getByCode(copyrights.getWorksStatus()).getDesc() + "</td>");
                content.append("<td>" + OnlineStatusEnum.getByCode(copyrights.getOnlineStatus()).getDesc() + "</td>");
                content.append("<td>" + DateUtil.format(copyrights.getBeginDate(), DateUtil.yyyy_MM_dd) + "</td>");
                content.append("<td>" + DateUtil.format(copyrights.getEndDate(), DateUtil.yyyy_MM_dd) + "</td>");
                content.append("<td>" + BalanceTypeEnum.getByCode(copyrights.getBalanceType()).getDesc() + "</td>");
                content.append("<td>" + TFEnum.getByCode(copyrights.getSoleFlag()).getDesc() + "</td>");
                content.append("<td>" + TFEnum.getByCode(copyrights.getSubRightsFlag()).getDesc() + "</td>");
                content.append("<td>" + ProvideEnum.getByCode(copyrights.getOriginalCopyrightsFlag()).getDesc() + "</td>");
                content.append("<td>" + PrintOutUtil.printOut(copyrights.getModifier()) + "</td>");
                content.append("<td>" + DateUtil.format(copyrights.getModifyTime(),DateUtil.yyyy_MM_dd_HH_mm_ss) + "</td>");

                content.append("<td><a href=\"http://kada.dumeiweiping.com/index.html#/copyrights/edit?id=" + copyrights.getId() + "\">" + "去操作" + "</a></td></tr>");
                // http://kada.dumeiweiping.com/index.html#/supplyContract/editIn?id=18080200001019224
            });
            content.append("</table>");
            SendEmailUtils.sendEmail(subject, content.toString(), to);
        }


    }

}
